package com.baizhi.action;

import com.baizhi.entity.Class;
import com.baizhi.entity.Tag;
import com.baizhi.service.ClassService;
import com.baizhi.service.impl.ClassServiceImpl;
import com.baizhi.service.impl.TagServiceImpl;
import com.baizhi.service.tagService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.sql.Date;
import java.util.List;
import java.util.UUID;

public class ClassAction extends ActionSupport {
    //班级对象
    Class clazz;
    int id;
    List<Class> classes;

    //接受数据
    private File upload;
    //接受文件的原始文件名 文件名.后缀  上传文件+"FileName"
    private String uploadFileName;
    private String realPath;

    List<Tag> clazzTags;




    /* // 查询所有班级标签 用于添加班级时选择标签*/
    public String queryClazzTags(){
        ClassService cs = new ClassServiceImpl();
        clazzTags = cs.queryClazzTag();

        return "queryClazzTags";
    }


    /* 添加城市 */
   public String insertClass() {
        String name=null;
        try {
            //调用业务  读写文件
            String path= ServletActionContext.getRequest().getSession().getServletContext().getRealPath(realPath);
            name  = UUID.randomUUID()+uploadFileName.substring(uploadFileName.lastIndexOf('.'));
            System.out.println(path);
            FileUtils.copyFile(upload,new File(path+"\\"+name));
        }catch (Exception e){
            e.printStackTrace();
        }

       ClassService cs = new ClassServiceImpl();
       clazz.setPath(name);
       cs.insertClass(clazz);

       return "insertsuccess";
   }




    /* //删除班级 */
    public String deleteClass(){

        ClassService cs = new ClassServiceImpl();
        cs.deleteById(id);

       return "delectsuccess";
    }


    /* //查询所有班级 */
    public String queryAllClasses(){

        ClassService cs = new ClassServiceImpl();
        classes = cs.queryAll();

        return "queryAllsuccess";
    }



    //get/set
    public List<Tag> getClazzTags() {
        return clazzTags;
    }

    public void setClazzTags(List<Tag> clazzTags) {
        this.clazzTags = clazzTags;
    }

    public Class getClazz() {
        return clazz;
    }

    public void setClazz(Class clazz) {
        this.clazz = clazz;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<Class> getClasses() {
        return classes;
    }

    public void setClasses(List<Class> classes) {
        this.classes = classes;
    }


    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }


    public String getRealPath() {
        return realPath;
    }

    public void setRealPath(String realPath) {
        this.realPath = realPath;
    }
}
